//
//  SISSConfirmIntentionLeaseViewController.m
//  SISSAPP
//
//  Created by 袁应荣 on 2019/7/18.
//  Copyright © 2019 优房网. All rights reserved.
//

#import "SISSConfirmIntentionLeaseViewController.h"

#import "SISSTenancyTenantTakeLookCell.h"
#import "SISSTenantTakeLookDetailViewController.h"
#import "SISSChatViewController.h"

#import "SISSNetworkingManager+TenantProgress.h"
#import "SISSNetworkingManager+Progress.h"
#import "SISSTenantTakeLookModel.h"
#import "SISSTenantProgressModel.h"
#import "SISSTenantProgressRecordInfoModel.h"
#import "SISSCommonRentCell.h"
#import "SISSRentOutDetailViewController.h"
#import "SISSUploadSuccessViewController.h"

static NSString * const rentOutCellReuseID = @"rentOutCellReuseID";
@interface SISSConfirmIntentionLeaseViewController ()<SISSTenancyTenantTakeLookCellDelegate>

/* 带看模型数组 */
@property (strong, nonatomic) NSArray<SISSTenantTakeLookModel *> *takeLookArr;

@property (strong, nonatomic) NSArray<SISSRentOutModel *> *rentOutArr;

@end

@implementation SISSConfirmIntentionLeaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = self.hasConfirm ? @"租赁房源" : @"确定租赁房源";
    
    if (self.hasConfirm) {
        // 注册cell
        [self.tableView registerClass:[SISSCommonRentCell class] forCellReuseIdentifier:rentOutCellReuseID];
        [self requestTradeLeaseInfoData];
    } else {
        [self.tableView registerClass:[SISSTenancyTenantTakeLookCell class] forCellReuseIdentifier:tenancyTenantTakeLookCellReuseID];
        [self requestTenantTakeLookRecordData];
        WeakSelf
        [kSISSRefresh prepareHeaderRefresh:self.tableView loadDataBlock:^{
            kSISSRefresh.refreshType = SISSRefressTypeUpDown;
            [weakSelf upDownLoadData];
        }];
    }
   
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)confirmButtonDidClickAction {
    [super confirmButtonDidClickAction];
    
    SISSTenantTakeLookModel *takeLookModel = self.takeLookArr[self.selectIndexPath.row];
    [self alertConfirmCacellActionAlertWithTitle:self.hasConfirm ? @"取消交易" : @"确定交易" message:self.hasConfirm ? @"您确定取消双方交易，并放弃后续已经开始的流程" : @"您确定双方已经达成一致购买意见并发起交易吗？" leftConfirmTitle:self.hasConfirm ? @"确定取消交易" : @"确定交易" rightConfirmTitle:@"取消" selectLeftBlock:^{
        if (self.hasConfirm) {
            SISSUploadSuccessViewController *vc = [[SISSUploadSuccessViewController alloc] init];
            vc.title = self.hasConfirm ? @"取消交易" : @"选择交易客户";
            vc.tipStr = self.hasConfirm ? @"交易已取消" : @"交易意向已锁定,请尽快完成合同签署";
            vc.popBackClassType = SISSProgressPopBackClassTypeTenant;
            [self.navigationController pushViewController:vc animated:YES];
        } else {
            NSDictionary *parameters = @{
                                         @"arbpId" : self.tenantProgressModel.arbpId,
                                         @"albpId" : takeLookModel.albpId.length > 0 ? takeLookModel.albpId : @"-1",
                                         @"alrId" : takeLookModel.alrId,
                                         @"leaseType" : @(takeLookModel.leaseType)
                                         };
            [kNetworkManager confirmTradeLeaseWithParameters:parameters success:^(id responseObject) {
                SISSUploadSuccessViewController *vc = [[SISSUploadSuccessViewController alloc] init];
                if ([responseObject[@"code"] integerValue] == 200) {
                    vc.title = self.hasConfirm ? @"取消交易" : @"选择交易客户";
                    vc.tipStr = self.hasConfirm ? @"交易已取消" : @"交易意向已锁定,请尽快完成合同签署";
                    vc.popBackClassType = SISSProgressPopBackClassTypeTenant;
                    [self.navigationController pushViewController:vc animated:YES];
                }
                
            } failed:^(NSError *error) {
                NSLog(@"%@",error);
            }];
        }
        
    } selectRightBlock:^{
        
    }];
}

#pragma mark - UITableViewDelegate & UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.hasConfirm ? self.rentOutArr.count : self.takeLookArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (self.hasConfirm) {
        SISSCommonRentCell *cell = [tableView dequeueReusableCellWithIdentifier:rentOutCellReuseID forIndexPath:indexPath];
        cell.isHideButton = YES;
        cell.rentOutModel = self.rentOutArr[indexPath.row];
        return cell;
    } else {
    
        SISSTenancyTenantTakeLookCell *cell = [tableView dequeueReusableCellWithIdentifier:tenancyTenantTakeLookCellReuseID forIndexPath:indexPath];
        cell.delegate = self;
        cell.tenantTakeLookModel = self.takeLookArr[indexPath.row];
        return cell;
    }
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    if (self.hasConfirm) {
        SISSRentOutDetailViewController *vc = [[SISSRentOutDetailViewController alloc] init];
        vc.title = @"出租详情";
        vc.isHiddenBottomButton = YES;
        vc.rentOutModel = self.rentOutArr[indexPath.row];
        [self.navigationController pushViewController:vc animated:YES];
    } else {
        SISSTenantTakeLookDetailViewController *vc = [[SISSTenantTakeLookDetailViewController alloc] init];
        vc.tenantProgressModel = self.tenantProgressModel;
        //    vc.brokerState = self.takeLookArr[indexPath.row].brokerState;
        vc.takeLookModel = self.takeLookArr[indexPath.row];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

#pragma mark -- SISSTenancyRentOutTakeLookCellDelegate
- (BOOL)tenancyTenantTakeLookRecordCell:(SISSTenancyTenantTakeLookCell *)cell checkButtonIsSelected:(BOOL)isSelected {
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    self.takeLookArr[self.selectIndexPath.row].isSelected = NO;
    self.takeLookArr[indexPath.row].isSelected = YES;
    self.selectIndexPath = indexPath;
    [self.tableView reloadData];
    self.isSelectMain = YES;
    
    return NO;
}


#pragma mark - 加载数据
- (void)upDownLoadData {
    
    [self requestTenantTakeLookRecordData];
}

#pragma mark -- 获取带看记录数据
- (void)requestTenantTakeLookRecordData {
    
    WeakSelf
    [kNetworkManager requestTenantTakelookRecordWithArbpId:self.tenantProgressModel.arbpId success:^(NSArray<SISSTenantTakeLookModel *> *takeLookArr) {
        weakSelf.takeLookArr = takeLookArr;
        
        [weakSelf.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
            if (takeLookArr.count * 160 > SCREEN_HEIGHT - kTopHeight - 120) {
                make.height.mas_equalTo(SCREEN_HEIGHT - kTopHeight - 120);
            } else {
                make.height.mas_equalTo(takeLookArr.count * 160);
            }
            
        }];
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            [weakSelf.tableView reloadData];
            [weakSelf.tableView.mj_header endRefreshing];
        }];
        
    } failed:^(NSError *error) {
        
        [weakSelf.tableView.mj_header endRefreshing];
        NSLog(@"%@",error);
    }];
}

- (void)requestTradeLeaseInfoData {
    WeakSelf
    [kNetworkManager requestTradeLeaseInfoWithArbpId:self.tenantProgressModel.arbpId success:^(SISSRentOutModel *rentOutInfoModel) {
    
        weakSelf.rentOutArr = rentOutInfoModel != nil ? @[rentOutInfoModel] : @[];
        CGFloat height = (rentOutInfoModel != nil && rentOutInfoModel.houseFeature.length > 0) ? 100 : 66;
            [weakSelf.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(1 * height);
            }];
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                [weakSelf.tableView reloadData];
                [weakSelf.tableView.mj_header endRefreshing];
            }];
        } failed:^(NSError *error) {
            NSLog(@"%@",error);
        }];
}


@end
